﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;
using SysAdmin._01_Config;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._01_Shop
{
    public partial class ShopForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(ShopForm));
        private IList<UserInfoAndShopInfoEntity> ShopList { get; set; }

        public ShopForm()
        {
            InitializeComponent();
        }

        private void ShopForm_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
        }

        private void ShopForm_Shown(object sender, EventArgs e)
        {
            LoadShops();
        }

        private void LoadShops()
        {
            try
            {
                ShopList = ShopService.QueryAllShopInfoAndUserInfo();
                var ls = ShopList.Select(x => ShopInfoAndUserInfo_2_MyShopInfoAndUserInfo(x)).ToList();
                dataGridView1.DataSource = ls;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private MyShopInfoAndUserInfo ShopInfoAndUserInfo_2_MyShopInfoAndUserInfo(UserInfoAndShopInfoEntity info)
        {
            return new MyShopInfoAndUserInfo
            {
                Id = info.ShopInfo.Id,
                CreateTime = info.ShopInfo.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                ShopManager = info.UserInfo.Name,
                ShopManagerUserId = info.UserInfo.Id,
                ShopName = info.ShopInfo.Name
            };
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.RowIndex >= 0)
                {
                    if (e.ColumnIndex == 5)
                    {
                        int shopManagerUserId = (int)dataGridView1.Rows[e.RowIndex].Cells[2].Value;
                        var form = new ShopUserForm();
                        form.ShopManagerUserId = shopManagerUserId;
                        form.ShopName = (string)dataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue;
                        form.ShowDialog();
                    }
                    else if (e.ColumnIndex == 6)
                    {
                        int shopManagerUserId = (int)dataGridView1.Rows[e.RowIndex].Cells[2].Value;
                        var form = new AddShopForm();
                        form.ParentShopManagerUserId = shopManagerUserId;
                        var dr = form.ShowDialog();
                        if (dr == DialogResult.OK)
                        {
                            LoadShops();
                        }
                    }
                    else if (e.ColumnIndex == 7)
                    {
                        int shopManagerUserId = (int)dataGridView1.Rows[e.RowIndex].Cells[2].Value;
                        if (shopManagerUserId != MyConfig.RootUserId)
                        {
                            if (UICommonTool.ShowConfirmDialog("确定要删除店铺吗？"))
                            {
                                int shopId = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                                bool b = ShopService.DeleteShop(Convert.ToInt32(shopId));
                                bool b2 = ShopUserService.DeleteShopUserByShopManagerUserId(Convert.ToInt32(shopManagerUserId));
                                if (b && b2)
                                {
                                    UICommonTool.ShowInfoDialog("删除店铺成功!");
                                    LoadShops();
                                }
                                else
                                {
                                    UICommonTool.ShowAlertDialog("删除店铺失败！");
                                }
                            }
                        }
                        else
                        {
                            UICommonTool.ShowAlertDialog("无法删除第一店铺！");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private class MyShopInfoAndUserInfo
        {
            public int Id { get; set; }

            public int ShopManagerUserId { get; set; }

            public string ShopManager { get; set; }

            public string CreateTime { get; set; }

            public string ShopName { get; set; }
        }
    }
}
